﻿using AutoMapper;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class RouterShopServices : BaseServices<RouterShopDao>, IRouterShopServices
    {
        IBaseRepository<RouterShopDao> _dal;
        IMapper _mapper;
        public RouterShopServices(IBaseRepository<RouterShopDao> dal, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
        }
        public async Task<PageModel<SearchRouterShopResponseDto>> GetPage(SearchRouterShopRequestDto requestDto)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select rs.*,u1.namec as create_names,u2.namec as update_names,");
            strSql.Append("r.names as router_name,c.names as customer_names,s.namec as shop_names ");
            strSql.Append("from oms_wms_router_shop rs ");
            strSql.Append("left join oms_ur_user u1 on rs.create_user=u1.id ");
            strSql.Append("left join oms_ur_user u2 on rs.update_user=u2.id ");
            strSql.Append("left join oms_wms_router r on rs.router_id=r.id ");
            strSql.Append("left join oms_crm_customer c on rs.customer_id=c.id ");
            strSql.Append("left join oms_crm_customer_shop s on rs.shop_id=s.id ");
            strSql.Append("where 1=1 ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            if (requestDto.router_id.HasValue && requestDto.router_id > 0)
            {
                strSql.Append("and rs.router_id=@router_id ");
                parameters.Add(new SugarParameter("@router_id", requestDto.router_id));
            }
            if (requestDto.customer_id.HasValue && requestDto.customer_id > 0)
            {
                strSql.Append("and rs.customer_id=@customer_id ");
                parameters.Add(new SugarParameter("@customer_id", requestDto.customer_id));
            }
            if (requestDto.shop_id .HasValue && requestDto.shop_id > 0)
            {
                strSql.Append("and rs.shop_id=@shop_id ");
                parameters.Add(new SugarParameter("@shop_id", requestDto.shop_id));
            }
            if (requestDto.status.HasValue && requestDto.status > 0)
            {
                strSql.Append("and rs.status=@status ");
                parameters.Add(new SugarParameter("@status", requestDto.status));
            }
            else
            {
                strSql.Append("and rs.status<3 ");
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timef))
            {
                strSql.Append("and rs.create_time>=@create_time_begin ");
                parameters.Add(new SugarParameter("@create_time_begin", requestDto.timef));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timet))
            {
                strSql.Append("and rs.create_time<@create_time_end ");
                parameters.Add(new SugarParameter("@create_time_end", Convert.ToDateTime(requestDto.timet).AddDays(1)));
            }
            return await base.SqlQueryPage<SearchRouterShopResponseDto>(strSql.ToString(), parameters, requestDto.pageIndex, requestDto.pageSize, " rs.expand_time ");
        }
     
    }
}
